#! /usr/bin/perl -I /etc/diagela
#
# Copyright (C) 2005 IBM Corporation
#
# Author: Michael Strosaker <strosake@us.ibm.com>
#
 
use Getopt::Long;

require "servevent_parse.pl";

sub usage {
	print "$0 [OPTIONS]\n";
	print "    --console: print notification to the console\n";
	print "    --email:   send notification to the specified addresses\n";
	print "               (comma-separated)\n";
	print "    --quiet:   do not print notification to stdout\n";
	print "    --syslog:  print notification to /var/log/messages\n";
	print "    --log:     print notification to specified log file\n";
	exit(0);
}

@lines = (
"-----------------------------------------------------------------------",
"",
"Automatic Error Log Analysis has detected a problem.",
""
);

Getopt::Long::Configure("bundling");
GetOptions("console|c"=>\$flag_console,
	"email|e=s"=>\$flag_email,
	"quiet|q"=>\$flag_quiet,
	"help|h"=>\$flag_help,
	"log|l=s"=>\$flag_logfile,
	"syslog|s"=>\$flag_syslog,
	"<>"=>\&bad_arg) or usage();

usage() if $flag_help;

($se_vars, $frus, $menutext) = parse_se();

# Generate the message text
@lines[1] = $se_vars->{"Date/Time"};
if (defined $se_vars->{"SRN"} || $se_vars->{"SRN_SRC"}) {
	push(@lines, "The Service Request Number(s)/Probable Cause(s)");
	push(@lines, "(causes are listed in descending order of probability):");
	push(@lines, "");
	push(@lines, $se_vars->{"SRN"}.substr($se_vars->{"SRN_SRC"}, 0, 8).
		": ".$se_vars->{"Text"});
	push(@lines, "");
}
if (defined $se_vars->{"Refc2"} && defined $se_vars->{"Refc3"} &&
		defined $se_vars->{"Refc4"} && defined $se_vars->{"Refc5"} &&
		defined $se_vars->{"Refc6"} && defined $se_vars->{"Refc7"} &&
		defined $se_vars->{"Refc8"} && defined $se_vars->{"Refc9"}) {
	push(@lines, "Additional words 2-".$se_vars->{"Refc2"}.
		" 3-".$se_vars->{"Refc3"}." 4-".$se_vars->{"Refc4"}.
		" 5-".$se_vars->{"Refc5"});
	push(@lines, "                 6-".$se_vars->{"Refc6"}.
		" 7-".$se_vars->{"Refc7"}." 8-".$se_vars->{"Refc8"}.
		" 9-".$se_vars->{"Refc9"});
	push(@lines, "");
}
if (defined $se_vars->{"Menu Number"}) {
	push(@lines, @$menutext);
	push(@lines, "");
}
foreach $fru (@$frus) {
	@data = split / /, $fru;
	if (scalar(@data) == 3) {	# FRU line
		$line = sprintf("Location: %-15s FRU: %-8s Ref-Code: %s",
			shift(@data), shift(@data), shift(@data));
		push(@lines, $line);
	}
	elsif (scalar(@data) == 7) {	# FRU_SRC line
		push(@lines, "Priority: ".shift(@data)." Type: ".shift(@data).
			" Procedure Id: ".shift(@data));
		push(@lines, "Location: ".shift(@data)." FRU: ".shift(@data).
			" Serial: ".shift(@data)." CCIN: ".shift(@data));
	}
	push(@lines, "");
}
if (defined $se_vars->{"ServiceLogID"}) {
	push(@lines, "Analysis of Service Log event number: ".
		$se_vars->{"ServiceLogID"});
}
if (defined $se_vars->{"Error Log Sequence Number"}) {
	push(@lines, "Analysis of platform log sequence number: ".
		$se_vars->{"Error Log Sequence Number"});
}

# Print to stdout, if appropriate
if (!$flag_quiet) {
	foreach $line (@lines) {
		print("$line\n");
	}
}

# Write to a log file, if appropriate
if ($flag_logfile) {
	if (! open(LOGFILE, ">> $flag_logfile")) {
		print("Could not open $flag_logfile");
	} else {
		foreach $line (@lines) {
			print(LOGFILE "diagela: $line\n");
		}
		close(LOGFILE);
	}
}

# Write to syslog, if appropriate
if ($flag_syslog) {
	if (!open(LOGPROG, "| logger -p local4.warning -t diagela")) {
		print("Could not run logger to notify syslog\n");
	}
	else {
		foreach $line (@lines) {
			print(LOGPROG "$line\n");
		}
		close(LOGPROG);
	}
}

# Print to console, if appropriate
if ($flag_console) {
	if (!open(CONSOLE, "> /dev/console")) {
		print("Could not open /dev/console\n");
	}
	else {
		foreach $line (@lines) {
			print(CONSOLE "$line\n");
		}
		close(CONSOLE);
	}
}

# Send out e-mail notifications, if appropriate
if ($flag_email) {
	$hostname = `hostname`;
	chomp $hostname;
	$subject = "diagela message from $hostname";

	if (!open(MAILLIST, "< /etc/diagela/mail_list")) {
		print("Could not open /etc/diagela/mail_list\n");
	}
	else {
		while (<MAILLIST>) {
			chomp;
			$pos = index($_, "#");
			if ($pos == 0) {
				# the line starts with a hash
				next;
			}
			elsif ($pos > 0) {
				# trim off everything past the hash
				$mailid = substr($_, 0, $pos);
			}
			elsif ($pos == -1) {
				$mailid = $_;
			}
			# trim leadng and tailing whitespace
			$mailid =~ s/^\s+//;
			$mailid =~ s/\s+$//;

			if ($mailid ne "") {
				push(@maillist, $mailid);
			}
		}
	}

	if (scalar(@maillist) == 0) {
		# mail to the root group unless results were sent to an HMC
		if (!$hmc) {
			$rootgroup = `grep ^root: /etc/group | cut -f4 -d:`;

			# $rootgroup is a comma-separated list
			@maillist = split /,/, $rootgroup;
		}
	}

	while ($maillist[0] eq "") { shift @maillist; }

	if (scalar(@maillist) > 0) {
		if (!open(MAILPROG, "| mail -n -s \"$subject\" ".
				join(",", @maillist))) {
			print("Could not run mail to deliver notifications\n");
		}
		else {
			foreach $line (@lines) {
				print(MAILPROG "$line\n");
			}
			close(MAILPROG);
		}
	}
}

